#include "pch.hpp"
#include "Callstack.hpp"

using namespace Engine;
using namespace Engine::Debugger;




Callstack::Callstack()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////

Callstack::Callstack(const std::vector<Script::Function>& stack)
{
	m_stack.reserve(stack.size());
	for(auto i = stack.begin(); i != stack.end(); ++i)
		m_stack.push_back(i->id());
}
///////////////////////////////////////////////////////////////////////////////////////////////////




std::vector<Script::Function> Callstack::functions(Script::Reflectable::ptr reflectable) const
{
	std::vector<Script::Function> functions;
	functions.reserve(m_stack.size());

	foreach(int id, m_stack)
		functions.emplace_back(reflectable->function(id));

	return functions;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




boostext::iostreams::bin_ostream& Engine::Debugger::operator<<(boostext::iostreams::bin_ostream& stream, const Callstack& callstack)
{
	stream << callstack.m_stack.size();
	for(auto i = callstack.m_stack.begin(); i != callstack.m_stack.end(); ++i)
		stream << *i;

	return stream;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

boostext::iostreams::bin_istream& Engine::Debugger::operator>>(boostext::iostreams::bin_istream& stream, Callstack& callstack)
{
	std::size_t n;
	stream >> n;
	callstack.m_stack.resize(n);
	for(std::size_t i = 0; i < n; ++i)
	{
		int id;
		stream >> id;
		callstack.m_stack.push_back(id);
	}

	return stream;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
